#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def InsertOrganization():
    import MySQLdb
    from pymongo import MongoClient
    import finalseg

    # MySQL Info
    DATABASE_NAME = 'pmc_meta'
    HOST = '10.10.31.17'
    PORT = '3306'
    USER_NAME = 'pmc'
    PASSWORD = 'pmc'
    CHAR_SET = 'utf8'

    # MongoDB Info
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "organization"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    conn = MySQLdb.connect(host=HOST, user=USER_NAME, passwd=PASSWORD, db=DATABASE_NAME, charset=CHAR_SET)
    cursor = conn.cursor()
    sql = "SELECT id,name,description FROM tb_dds_organization"
    cursor.execute(sql)
    for row in cursor.fetchall():
        record = {}
        id = row[0]
        name = row[1]
        description = row[2]
        segment = []
        segs = finalseg.cut(name)
        for seg in segs:
            segment.append(seg)
        record["org_id"] = id
        record["FullName"] = name
        record["Description"] = description
        record["Segment"] = segment
        collection.insert_one(record)
    connection.close()
    cursor.close()
    conn.close()


if __name__ == "__main__":
    InsertOrganization()
